package zjc.bookkeeping.minepage.cClassificationmanagement;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputFilter;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;


import java.util.List;

import zjc.bookkeeping.R;
import zjc.bookkeeping.db.ConsumptionClassBean;
import zjc.bookkeeping.db.DBManager;

public class ClassificationmanagementMainActivity extends Activity {

    private ExpandableListView elvBook;
    private ClassificationListAdapter adapter;

    ConsumptionClassBean consumptionClassBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_classificationmanagement_main);

        //classname();
        showlist();
    }
    //返回侧边导航栏
    public void back(View v){
       finish();
    }
    //查询数据
    private int[][] classnameID(){
        List<ConsumptionClassBean> list1 = DBManager.getConsumptionClass(0);
        List<ConsumptionClassBean> list2 = DBManager.getConsumptionClass(1);
        int[] id1=new int[list1.size()];
        int[] id2=new int[list2.size()];
        int k1 = list1.size();
        int k2 = list2.size();
        Log.i("1.k", String.valueOf(k1));
        Log.i("2.k", String.valueOf(k2));
        for (int i = 0;i<k1;i++) {
            id1[i] = list1.get(i).getConsumption_class_id();
         }
        for (int i = 0;i<k2;i++) {
            id2[i] = list2.get(i).getConsumption_class_id();
          }
        return new int[][]{id1, id2};
    }
    private String[][] classname(){
        List<ConsumptionClassBean> list1 = DBManager.getConsumptionClass(0);
        List<ConsumptionClassBean> list2 = DBManager.getConsumptionClass(1);
        int k1 = 0 ;
        int k2 = 0;
        String[] text1 = new String[list1.size()];
        String[] text2 = new String[list2.size()];
        k1 = list1.size();
        k2 = list2.size();
        Log.i("1.k", String.valueOf(k1));
        Log.i("2.k", String.valueOf(k2));
        for (int i = 0;i<k1;i++) {
            String name = list1.get(i).getConsumption_class_name();
            text1[i] = name;
            System.out.println(list1.get(i).getConsumption_class_id() + "账户:" + list1.get(i).getConsumption_class_kind() + list1.get(i).getConsumption_class_name() + list1.get(i).getConsumption_class_image());
        }
        for (int i = 0;i<k2;i++) {
            String name = list2.get(i).getConsumption_class_name();
            text2[i] = name;
            System.out.println(list2.get(i).getConsumption_class_id() + "账户:" + list2.get(i).getConsumption_class_kind() + list2.get(i).getConsumption_class_name() + list2.get(i).getConsumption_class_image());
        }
        return new String[][]{text1, text2};
    }

    //可折叠列表
    void showlist(){
        String[] groups = {"支出", "收入"};
        String[][] children = classname();
        // 通过资源标识获得控件实例
        elvBook = findViewById(R.id.elvBook);

        // 创建适配器
        adapter = new ClassificationListAdapter(this, groups, children);

        // 给列表控件设置适配器
        elvBook.setAdapter(adapter);

        // 给列表控件注册组列表展开监听器（非当前组收拢）
        elvBook.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
                for (int i = 0;  i < groups.length; i++) {
                    if (i != groupPosition) {
                        elvBook.collapseGroup(i);
                    }
                }
            }
        });

        // 给列表控件注册组列表单击事件监听器
        elvBook.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView expandableListView, View view, int groupPosition, long l) {
//                Toast.makeText(ClassificationmanagementMainActivity.this, "你单击了第[" + (groupPosition + 1) + "]组：" + groups[groupPosition], Toast.LENGTH_SHORT).show();
                // 事件可往后传播
                return false;
            }
        });

        // 给列表控件注册子列表单击事件监听器
//        elvBook.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
//            int[][] classnameId = classnameID();
//            @Override
//            public boolean onChildClick(ExpandableListView expandableListView, View view, int groupPosition, int childPosition, long childId) {
////                Toast.makeText(ClassificationmanagementMainActivity.this, "你现在在对第[" + (groupPosition + 1) + "]组第[" + (childPosition + 1) + "]项：" + children[groupPosition][childPosition]+"进行删除/修改操作，点击空白即可返回", Toast.LENGTH_SHORT).show();
//                int id=classnameId[groupPosition][childPosition];
//                consumptionClassBean = new ConsumptionClassBean();
//                consumptionClassBean.setConsumption_class_id(id);
////                final int[] kind = {0};
////                final TextView inputServer = new TextView(ClassificationmanagementMainActivity.this);
////                inputServer.setFilters(new InputFilter[]{new InputFilter.LengthFilter(50)});
////                inputServer.setText(children[groupPosition][childPosition]);
//                AlertDialog.Builder builder = new AlertDialog.Builder(ClassificationmanagementMainActivity.this);
//                builder.setTitle("删除分类").setIcon(R.drawable.func_classification_management);
//                builder.setNegativeButton("删除", new DialogInterface.OnClickListener(){
//
//                    @Override
//                    public void onClick(DialogInterface dialogInterface, int i) {
//                        DBManager.deleteConsumptionClassbyid(id);
//                        onCreate(null);
//                    }
//                });
//                builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
//                    public void onClick(DialogInterface dialog, int which) {
//                    }
//                });
//
//                builder.show();
//                // 禁止事件往后传播
//                return true;
//            }
//        });
    }

    public void add(View v){
//        AddDialog addDialog = new AddDialog(ClassificationmanagementMainActivity.this);
//        addDialog.show();
//        addDialog.setDialogSize();
//        addDialog.setAddSure(new AddDialog.AddSure() {
//            @Override
//            public void onSureListener() {
//                onCreate(null);
//            }
//        });
        Toast.makeText(ClassificationmanagementMainActivity.this, "该功能暂未完成！", Toast.LENGTH_SHORT).show();

    }

}